package com.thinkgem.jeesite.modules.building.web;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.building.entity.Building;
import com.thinkgem.jeesite.modules.building.entity.Room;
import com.thinkgem.jeesite.modules.building.service.BuildingService;
import com.thinkgem.jeesite.modules.building.service.RoomService;
import com.thinkgem.jeesite.modules.customer.service.OwnerService;
import com.thinkgem.jeesite.modules.fee.entity.Fee;
import com.thinkgem.jeesite.modules.fee.service.FeeService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

@Controller
@RequestMapping(value="${adminPath}/admin/building")
public class BuildingController extends BaseController{
	@Autowired
	private BuildingService buildingservice;
	@Autowired
	private SystemService systemservice;
	@Autowired
	private OwnerService ownerservice;
	@Autowired
	private RoomService roomservice;
	@Autowired
	private FeeService feeservice;

	
	/**
	 * 
	 * */
	@RequestMapping(value="buildingList")
	public String buildingList(HttpServletResponse response,HttpServletRequest request,Model model){
		Page<Building> page = buildingservice.findPage(new Page<Building>(request,response) ,new Building());
		List<Building> find = page.getList();
		Building b = new Building();
		for(int i=0;i<find.size();i++){
			String[] admins = find.get(i).getAdminsId().split(",");
			for(int n=0;n<admins.length;n++){
				if(n ==0) page.getList().get(i).setAdminsId("");  //清空
				if(systemservice.getUser(admins[n])!=null){
					String name = ","+systemservice.getUser(admins[n]).getName();
					String beforeName = page.getList().get(i).getAdminsId();
					page.getList().get(i).setAdminsId(beforeName+name);
				}
			}
		}
		model.addAttribute("page", page);
		return "modules/building/buildingList";
	}
	/**
	 * 
	 * */
	public boolean isInArray(String[] arr,String str){
		for(int i=0;i<arr.length;i++){
			if(arr[i].equals(str)) return true;
		}
		return false;
	}
	@RequestMapping(value="MybuildingList")
	public String MybuildingList(HttpServletResponse response,HttpServletRequest request,Model model){
		Page<Building> page = buildingservice.findMyList(new Page<Building>(request,response) ,new Building());
		List<Building> find = page.getList();
		Building b = new Building();
		for(int i=0;i<find.size();i++){
			String[] admins = find.get(i).getAdminsId().split(",");
			for(int n=0;n<admins.length;n++){
				if(n ==0) page.getList().get(i).setAdminsId("");  //清空
				if(systemservice.getUser(admins[n])!=null){
					String name = ","+systemservice.getUser(admins[n]).getName();
					String beforeName = page.getList().get(i).getAdminsId();
					page.getList().get(i).setAdminsId(beforeName+name);
				}
			}
		}
		model.addAttribute("page", page);
		return "modules/building/MybuildingList";
	}
	@RequestMapping(value="adminsList")
	public String adminsList(HttpServletRequest request,HttpServletResponse response,Model model,User user){
		model.addAttribute("teacherids", "");
		Page<User> result = systemservice.findAdminList(new Page<User>(request,response),user);
		for(int i=0;i<result.getList().size();i++){
			if(result.getList().get(i).getIsUser()==null || result.getList().get(i).getIsUser().equals("1")) result.getList().remove(i);
		}
		model.addAttribute("user", user);
		model.addAttribute("admin", result);
		return "modules/building/adminList";
	}
	@RequestMapping(value="addBuilding")
	public String addBuilding(Model model){
		model.addAttribute("building", new Building());
		return "modules/building/buildingAdd";
	}
	@RequestMapping(value="insertBuilding")
	public String insertBuilding(Building building,Model model){
		building.setId(UUID.randomUUID().toString());
		//System.out.println(building.toString());
		buildingservice.insertBuilding(building);
		addMessage(model, "添加楼宇成功");
		return "redirect:"+Global.getAdminPath()+"/admin/building/buildingList";
	}
	@RequestMapping(value="buildingForm")
	public String buildingForm(@RequestParam("buildingId")String buildingId,Model model){
		Building building = buildingservice.get(buildingId);
		List<User> userResult = new ArrayList<User>();
		String[] admins = building.getAdminsId().split(",");
		for(int i=0;i<admins.length;i++){
			userResult.add(systemservice.getUser(admins[i]));
		}
		model.addAttribute("user", userResult);
		model.addAttribute("building", building);
		return "modules/building/buildingForm";
	}
	@RequestMapping("deleteAdmin")
	public String deleteAdmin(HttpServletRequest request,HttpServletResponse response,Model model,
			@RequestParam("buildingId")String buildingId,@RequestParam("adminId")String adminId){
		Building building = buildingservice.get(buildingId);
		String[] admins = building.getAdminsId().split(",");
		String result = "";
		for(int i=0;i<admins.length;i++){
			if(!admins[i].equals(adminId)){
				result+=admins[i]+",";
			}
		}
		building.setAdminsId(result);
		buildingservice.updateAdminsId(building);
		return "redirect:"+Global.getAdminPath()+"/admin/building/buildingList";
	}
	@RequestMapping("addAdmin")
	public String addAdmin(HttpServletRequest request,HttpServletResponse response,Model model,Building building){
		buildingservice.updateAdminsId(building);
		return "redirect:"+Global.getAdminPath()+"/admin/building/buildingList";
	}
	@RequestMapping(value="RoomList")
	public String RoomList(HttpServletRequest request,HttpServletResponse response,Model model){
		Page<Room> page = roomservice.findPage(new Page<Room>(request,response) ,new Room());
		model.addAttribute("page", page);
		return "modules/building/roomList";
	}
	@RequestMapping(value="myRoomList")
	public String myRoomList(HttpServletRequest request,HttpServletResponse response,Model model){
		Page<Room> page = roomservice.findMyAdminRoom(new Page<Room>(request,response) ,new Room());
		model.addAttribute("page", page);
		return "modules/building/roomList";
	}
	@RequestMapping(value="deleteRoom")
	public String deleyeRoom(HttpServletRequest request,HttpServletResponse response,Model model,String id){
		roomservice.deleteRoom(id);
		return "redirect:"+Global.getAdminPath()+"/admin/building/RoomList";
	}
	/**
	 * 删除楼宇，这是联动删除
	 * */
	@RequestMapping(value="deleteBuilding")
	public String deleteBuilding(HttpServletRequest request,HttpServletResponse response,@RequestParam("id")String id){
		List<Room> roomList = roomservice.findRoomsByBuildingId(id);
		for(int i=0;i<roomList.size();i++){
			Fee fee = feeservice.findFeeByRoomIds(roomList.get(i).getId());   //找到这个房间所对应的费率
			/*去除该费率中房间拼接字段，并更新*/
			String[] roomIds = fee.getRoomIds().split(",");
			String temp = "";
			for(String s:roomIds){
				if(!s.equals(roomList.get(i).getId())) temp+=s+",";
			}
			fee.setRoomIds(temp);
			feeservice.undateRoomIds(fee);
			feeservice.deleteUnfeeByUserId(systemservice.getUser(roomList.get(i).getUser().getId()).getId());  //欠费表也删除
			roomservice.deleteRoom(roomList.get(i).getId());   //删除这个房间
			buildingservice.deleteBuildingById(id);
			feeservice.deleteFeeByBuildingId(id);
			systemservice.deleteUser(systemservice.getUser(roomList.get(i).getUser().getId()));  //房间不存在那么用户也没有存在的价值了
		}
		return "redirect:"+Global.getAdminPath()+"/admin/building/buildingList";
	}
	
	
}
